import axios from 'axios';
import Qs from 'qs';

/* axios配置 */
var instance = axios.create({
    timeout: 20000,
    headers: {
        'Accept': 'application/json'
    }
});

// 添加一个请求拦截器
instance.interceptors.request.use(config => {
    return config;
}, err => {
    return Promise.reject(err);
});

// 添加一个返回拦截器
instance.interceptors.response.use(config => {
    return config;
}, err => {
    if (err.message.includes('timeout')) {   // 判断请求异常信息中是否含有超时timeout字符串
        
        return Promise.reject(err);          // reject这个错误信息
    }
    return Promise.reject(err);
});

//返回一个Promise(默认发送post请求)
/**
 * option.method  类型  post
 * option.url   地址
 * params 传参
 * **/

const http = (option, params) => {
    if (typeof option == 'undefined') {
        option = {};
    }
    option.method = option.method || 'post';
    params.key = localStorage.getItem('key') ? localStorage.getItem('key') : '';
    if (params.noKey) {
        params.key = '';
    }
    return new Promise((resolve, reject) => {
        instance({ method: option.method, url: option.url, data: Qs.stringify(params) }).then(response => {
            if (response.data.code == 300 || response.data.code == 302) {
                console.log(option.url);
                console.log('需要跳转至登录');
                
            } else {
                resolve(response.data);
            }
        }, err => {
            reject(err);
        })
            .catch((error) => {
                reject(error)
            })
    })
}
const http_json = (option, params) => {
    let config = {
        headers: {
            'Content-Type': 'application/json',
        }
    }
    return new Promise((resolve, reject) => {
        axios.post(option.url, params, config).then(response => {
            if (response.data.code == 300 || response.data.code == 302) {
                console.log('需要跳转至登录');
                
            } else {
                resolve(response.data);
            }
        }, err => {
            reject(err);
        }).catch((error) => {
            reject(error)
        })
    })
};
const fileHttp = (option, params) => {
    let config = {
        headers: { 'Content-Type': 'multipart/form-data' }
    }
    return new Promise((resolve, reject) => {
        axios.post(option.url, params, config).then(response => {
            if (response.data.code == 300 || response.data.code == 302) {
                console.log('需要跳转至登录');
                
            } else {
                resolve(response.data);
            }
        }, err => {
            reject(err);
        }).catch((error) => {
            reject(error)
        })
    })
};
const http_get = {
    get(url, data) {
        data.key = localStorage.getItem('key') ? localStorage.getItem('key') : '';
        return new Promise((resolve, reject) => {
            axios.get(url, { params: data }).then(response => {
                if (response.data.code == 300 || response.data.code == 302) {
                    console.log('需要跳转至登录');
                    
                } else {
                    resolve(response.data);
                }
            }, err => {
                reject(err)
            }).catch((error) => {
                reject(error)
            })
        })
    }
};
export {
    axios,
    http,
    fileHttp,
    http_json,
    http_get
}